package com.centling.smartSealForPhone.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.InputFilter
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.LogUtil
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.centling.smartSealForPhone.utils.TextLengthWatch
import kotlinx.android.synthetic.main.activity_auditor_first_instance.*
import java.util.*

/**
 * Created by Jessie on 2016/8/17.
 */
class AuditorFirstInstanceActivity : TitleBarActivity() {
    private lateinit var applySealId:String

    private var reason: String = ""
    private var status: String = "1"
    private var applySealApprovalId = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleBarText("审批")
        setContentView(R.layout.activity_auditor_first_instance)

        applySealId = intent.getStringExtra("sealApplyId")

        radioGroupListener()

        tv_auditor_first_instance_ok.setOnClickListener {
            sendRequest()
        }
        tv_auditor_first_instance_cancel.setOnClickListener { finish() }

        et_auditor_first_instance_reject.filters = arrayOf<InputFilter>(android.text.InputFilter.LengthFilter(100))//限制字数

        et_auditor_first_instance_reject.addTextChangedListener(TextLengthWatch(100))//字数提醒

        et_auditor_first_instance_reject.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                tv_auditor_first_instance_words_limnit.text = String.format(getString(R.string.number_limit_100, s.toString().length))
                if (s.toString().length == 100) {
                    tv_auditor_first_instance_words_limnit.setTextColor(Color.RED)
                } else {
                    tv_auditor_first_instance_words_limnit.setTextColor(Color.BLACK)
                }
            }
        })

        et_auditor_first_instance_refuse.filters = arrayOf<InputFilter>(android.text.InputFilter.LengthFilter(100))//限制字数

        et_auditor_first_instance_refuse.addTextChangedListener(TextLengthWatch(100))//字数提醒

        et_auditor_first_instance_refuse.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                tv_auditor_instance_refuse_words_limnit.text = String.format(getString(R.string.number_limit_100, s.toString().length))
                if (s.toString().length == 100) {
                    tv_auditor_instance_refuse_words_limnit.setTextColor(Color.RED)
                } else {
                    tv_auditor_instance_refuse_words_limnit.setTextColor(Color.BLACK)
                }
            }
        })

        et_auditor_first_instance_approval.filters = arrayOf<InputFilter>(android.text.InputFilter.LengthFilter(100))//限制字数

        et_auditor_first_instance_approval.addTextChangedListener(TextLengthWatch(100))//字数提醒

        et_auditor_first_instance_approval.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                tv_auditor_instance_approval_words_limnit.text = String.format(getString(R.string.number_limit_100, s.toString().length))
                if (s.toString().length == 100) {
                    tv_auditor_instance_approval_words_limnit.setTextColor(Color.RED)
                } else {
                    tv_auditor_instance_approval_words_limnit.setTextColor(Color.BLACK)
                }
            }
        })

        ll_auditor_approval_trailer.setOnClickListener {
            startActivityForResult(Intent(mContext, AuditorApprovalActivity::class.java)
                    .putExtra("which_auditor_apply_seal_id", applySealId), AUDITOR_APPROVAL)
        }
    }

    private fun radioGroupListener() {
        rg_auditor_instance_type.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.rb_auditor_ratify -> {
                    ll_auditor_reject.visibility = View.GONE
                    ll_auditor_refuse.visibility = View.GONE
                    ll_auditor_approval.visibility = View.GONE
                    status = "1"
                }
                R.id.rb_auditor_refuse -> {
                    ll_auditor_refuse.visibility = View.VISIBLE
                    ll_auditor_reject.visibility = View.GONE
                    ll_auditor_approval.visibility = View.GONE
                    status = "2"
                }
                R.id.rb_auditor_reject -> {
                    ll_auditor_refuse.visibility = View.GONE
                    ll_auditor_reject.visibility = View.VISIBLE
                    ll_auditor_approval.visibility = View.GONE
                    status = "9"
                }
                R.id.rb_auditor_approval -> {
                    ll_auditor_refuse.visibility = View.GONE
                    ll_auditor_reject.visibility = View.GONE
                    ll_auditor_approval.visibility = View.VISIBLE
                    status = "-1"
                }
            }
        }
    }

    fun sendRequest() {
        if (rb_auditor_ratify.isChecked) {

        } else if (rb_auditor_refuse.isChecked) {
            reason = et_auditor_first_instance_refuse.text.toString()
            if (TextUtils.isEmpty(reason)) {
                ShowToast.shortTime("请输入原因")
                return
            }
        } else if (rb_auditor_reject.isChecked) {
            reason = et_auditor_first_instance_reject.text.toString()
            if (TextUtils.isEmpty(reason)) {
                ShowToast.shortTime("请输入原因")
                return
            }
        } else if (rb_auditor_approval.isChecked) {
            reason = et_auditor_first_instance_approval.text.toString()
            if (TextUtils.isEmpty(applySealApprovalId)) {
                ShowToast.shortTime("请选择审核人")
                return
            }
            if (TextUtils.isEmpty(reason)) {
                ShowToast.shortTime("请输入审阅意见")
                return
            }
        }

        val params = HashMap<String, String>()
        params["sealApplyId"] = applySealId
        params["status"] = status
        params["authFailReason"] = reason
        params["curTaster"] = applySealApprovalId
        params["applyAuditRemark"] = reason
        mProcessDialog.setTitle("正在审批").showNormal()
        putJsonEnqueue(UrlConstants.FIRST_CHECK, params, object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String?) {
                mProcessDialog.dismiss()
                ShowToast.shortTime("审批完成")
                finish()
            }

            override fun onFailed(reason: String?) {
                mProcessDialog.dismiss()
                ShowToast.shortTime(reason)
                finish()
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AUDITOR_APPROVAL && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                applySealApprovalId = data.getStringExtra("which_auditor_approval_id")
                val name = data.getStringExtra("which_auditor_approval_name")
                tv_auditor_approval.text = name
                LogUtil.d("选择审核人 $applySealApprovalId $name")
            }
        }
    }

    companion object {
        private const val AUDITOR_APPROVAL = 1001
    }
}
